package org.tong.web.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.tong.common.domain.dos.ArticleCategoryRelDO;
import org.tong.common.domain.dos.ArticleDO;
import org.tong.common.domain.dos.CategoryDO;
import org.tong.common.domain.mapper.ArticleCategoryRelMapper;
import org.tong.common.domain.mapper.ArticleMapper;
import org.tong.common.domain.mapper.CategoryMapper;
import org.tong.common.enums.ResponseCodeEnum;
import org.tong.common.exception.BizException;
import org.tong.common.utils.PageResponse;
import org.tong.common.utils.Response;
import org.tong.web.model.vo.category.FindCategoryArticlePageListReqVO;
import org.tong.web.model.vo.category.FindCategoryArticlePageListRspVO;
import org.tong.web.model.vo.category.FindCategoryListRspVO;
import org.tong.web.service.CategoryService;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ArticleCategoryRelMapper articleCategoryRelMapper;
    @Autowired
    private ArticleMapper articleMapper;
    /**
     * 获取分类列表
     *
     * @return
     */
    @Override
    public Response findCategoryList() {
        // 查询所有分类
        List<CategoryDO> categoryDOS = categoryMapper.selectList(Wrappers.emptyWrapper());

        // DO 转 VO
        List<FindCategoryListRspVO> vos = null;
        if (!CollectionUtils.isEmpty(categoryDOS)) {
            vos = categoryDOS.stream()
                    .map(categoryDO -> FindCategoryListRspVO.builder()
                            .id(categoryDO.getId())
                            .name(categoryDO.getName())
                            .build())
                    .collect(Collectors.toList());
        }
        return Response.success(vos);
    }

    @Override
    public Response findCategoryArticlePageList(FindCategoryArticlePageListReqVO findCategoryArticlePageListReqVO) {
        Long current = findCategoryArticlePageListReqVO.getCurrent();
        Long size = findCategoryArticlePageListReqVO.getSize();
        Long categoryId = findCategoryArticlePageListReqVO.getId();
        CategoryDO categoryDO = categoryMapper.selectById(categoryId);

        // 判断该分类是否存在
        if (Objects.isNull(categoryDO)) {
            log.warn("==> 该分类不存在, categoryId: {}", categoryId);
            throw new BizException(ResponseCodeEnum.CATEGORY_NOT_EXISTED);
        }
        List<ArticleCategoryRelDO> articleCategoryRelDOS = articleCategoryRelMapper.selectListByCategoryId(categoryId);
        // 若该分类下未发布任何文章
        if (CollectionUtils.isEmpty(articleCategoryRelDOS)) {
            log.info("==> 该分类下还未发布任何文章, categoryId: {}", categoryId);
            return PageResponse.success(null, null);
        }
        List<Long> articleIds = articleCategoryRelDOS.stream().map(ArticleCategoryRelDO::getArticleId).collect(Collectors.toList());

        Page<ArticleDO> articleDOPage = articleMapper.selectPageListByArticleIds(current, size, articleIds);
        List<ArticleDO> articleDOS = articleDOPage.getRecords();
        List<FindCategoryArticlePageListRspVO> vos = Lists.newArrayList();
        articleDOS.forEach((articleDO)->{
            vos.add(FindCategoryArticlePageListRspVO.builder()
                    .id(articleDO.getId())
                    .cover(articleDO.getCover())
                    .title(articleDO.getTitle())
                    .createDate(articleDO.getCreateTime().toLocalDate())
                    .build());
        });
        return PageResponse.success(articleDOPage,vos);
    }
}
